package com.lugea.esdemo.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author enluw
 * @data 2019/5/21
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @RequestMapping("/1")
    public Object test1() {
        SearchRequest searchRequest = new SearchRequest("kibana_sample_data_ecommerce");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.size(2);
        searchSourceBuilder.from(0);
        searchRequest.source(searchSourceBuilder);
        searchRequest.types("_doc");
        try {
            SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            return Arrays.stream(search.getHits().getHits()).map(hit -> hit.getSourceAsMap())
                    .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
